/*********************************************************************************
  *Copyright(C),2015-2020, 
  *TUYA 
  *FileName: tuya_ipc_dp_utils.c
  *
  * File Description：
  * 1. API implementation of DP point
  *
  * This file code is the basic code, users don't care it
  * Please do not modify any contents of this file at will. 
  * Please contact the Product Manager if you need to modify it.
  *
**********************************************************************************/
#include "tuya_ipc_dp_handler.h"
#include "tuya_ipc_dp_utils.h"
#include "tuya_iot_com_api.h"
#include "tuya_iot_config.h"

#define DEF_TAG             "TuyaDpUtil"

#ifdef TUYA_DP_SD_STORAGE_ONLY_GET
STATIC VOID handle_DP_SD_STORAGE_ONLY_GET(IN TY_OBJ_DP_S *p_obj_dp);
#endif /* end of TUYA_DP_SD_STORAGE_ONLY_GET */

extern UCHAR_T g_TY_sync_time_ok;

//------------------------------------------
VOID IPC_APP_upload_all_status(VOID)
{
#ifdef TUYA_DP_SLEEP_MODE
    respone_dp_bool(TUYA_DP_SLEEP_MODE, IPC_APP_get_sleep_mode());
#endif

#ifdef TUYA_DP_LIGHT
    respone_dp_bool(TUYA_DP_LIGHT, IPC_APP_get_light_onoff());
#endif

#ifdef TUYA_DP_FLIP
    respone_dp_bool(TUYA_DP_FLIP, IPC_APP_get_flip_onoff());
#endif

#ifdef TUYA_DP_WATERMARK
    respone_dp_bool(TUYA_DP_WATERMARK, IPC_APP_get_watermark_onoff());
#endif

#ifdef TUYA_DP_NIGHT_MODE
    respone_dp_enum(TUYA_DP_NIGHT_MODE, IPC_APP_get_night_mode());
#endif

#ifdef TUYA_DP_ALARM_FUNCTION
    respone_dp_bool(TUYA_DP_ALARM_FUNCTION, IPC_APP_get_alarm_function_onoff());
#endif

#ifdef TUYA_DP_ALARM_SENSITIVITY
    respone_dp_enum(TUYA_DP_ALARM_SENSITIVITY, IPC_APP_get_alarm_sensitivity());
#endif

#ifdef TUYA_DP_ALARM_ZONE_ENABLE
    respone_dp_bool(TUYA_DP_ALARM_ZONE_ENABLE, IPC_APP_get_alarm_zone_onoff());
#endif

#ifdef TUYA_DP_ALARM_ZONE_DRAW
    respone_dp_str(TUYA_DP_ALARM_ZONE_DRAW, IPC_APP_get_alarm_zone_draw());
#endif

#ifdef TUYA_DP_ALARM_INTERVAL
    respone_dp_enum(TUYA_DP_ALARM_INTERVAL, IPC_APP_get_alarm_interval());
#endif

#ifdef TUYA_DP_SD_STATUS_ONLY_GET
    respone_dp_value(TUYA_DP_SD_STATUS_ONLY_GET, IPC_APP_get_sd_status());
#endif

#ifdef TUYA_DP_SD_STORAGE_ONLY_GET
    handle_DP_SD_STORAGE_ONLY_GET(NULL);
#endif

#ifdef TUYA_DP_SD_RECORD_ENABLE
    respone_dp_bool(TUYA_DP_SD_RECORD_ENABLE, IPC_APP_get_sd_record_onoff());
#endif

#ifdef TUYA_DP_SD_RECORD_MODE
    CHAR_T sd_mode[4];
    snprintf(sd_mode,4,"%d",IPC_APP_get_sd_record_mode());
    respone_dp_enum(TUYA_DP_SD_RECORD_MODE, sd_mode);
#endif

#ifdef TUYA_DP_SD_FORMAT_STATUS_ONLY_GET
    respone_dp_value(TUYA_DP_SD_FORMAT_STATUS_ONLY_GET, 0);
#endif

#ifdef TUYA_DP_SET_SPK_VOLUME
    respone_dp_value(TUYA_DP_SET_SPK_VOLUME, IPC_APP_get_spk_volume());
#endif

#ifdef TUYA_DP_IPC_CONTRAST
    respone_dp_value(TUYA_DP_IPC_CONTRAST, IPC_APP_get_ipc_contrast());
#endif

#ifdef TUYA_DP_IPC_BRIGHT
    respone_dp_value(TUYA_DP_IPC_BRIGHT, IPC_APP_get_ipc_bright());
#endif

#ifdef TUYA_DP_RECORDING_EXISTS
    CHAR_T record_exists[2] = {0};
    snprintf(record_exists,2,"%d",IPC_APP_get_recording_exists());
    respone_dp_enum(TUYA_DP_RECORDING_EXISTS, record_exists);
#endif

#ifdef TUYA_DP_RECORD_STATE
    CHAR_T record_state[2] = "1";
    respone_dp_enum(TUYA_DP_RECORD_STATE, record_state);
#endif

#ifdef TUYA_DP_MOTION_TIMER_SWITCH
    respone_dp_bool(TUYA_DP_MOTION_TIMER_SWITCH, IPC_APP_get_motion_timer_switch());
#endif

#ifdef TUYA_DP_MOTION_TIMER_SETTING
    CHAR_T md_time[12] = {0};

    IPC_APP_get_motion_timer_setting(md_time);
    respone_dp_str(TUYA_DP_MOTION_TIMER_SETTING, md_time);
#endif

#ifdef TUYA_DP_RGB_LED_COLOUR
    respone_dp_value(TUYA_DP_RGB_LED_COLOUR, IPC_APP_get_rgb_led_colour());
#endif
}

#ifdef TUYA_DP_SD_FORMAT_STATUS_ONLY_GET
VOID IPC_APP_report_sd_format_status(INT_T status)
{
    respone_dp_value(TUYA_DP_SD_FORMAT_STATUS_ONLY_GET, status);
}
#endif

#ifdef TUYA_DP_SD_STATUS_ONLY_GET
VOID IPC_APP_report_sd_status_changed(INT_T status)
{
    respone_dp_value(TUYA_DP_SD_STATUS_ONLY_GET, status);
}
#endif

#ifdef TUYA_DP_SD_STORAGE_ONLY_GET
VOID IPC_APP_report_sd_storage()
{
    CHAR_T tmp_str[128] = {0};

    UINT_T total = 100;
    UINT_T used = 0;
    UINT_T empty = 100;
    IPC_APP_get_sd_storage(&total, &used, &empty);

    //"total capacity|Current usage|remaining capacity"
    snprintf(tmp_str, 100, "%d|%d|%d", total, used, empty);
    respone_dp_str(TUYA_DP_SD_STORAGE_ONLY_GET, tmp_str);
}
#endif

//------------------------------------------
VOID async_respone_dp_value(BYTE_T dp_id, INT_T val)
{
    if(g_TY_sync_time_ok == DEF_COM_FALSE)
    {
        return;
    }

    tuya_ipc_dp_report(NULL, dp_id,PROP_VALUE,&val,1);
}

VOID respone_dp_value(BYTE_T dp_id, INT_T val)
{
    if(g_TY_sync_time_ok == DEF_COM_FALSE)
    {
        return;
    }

    tuya_ipc_dp_report(NULL, dp_id,PROP_VALUE,&val,1);
}

VOID respone_dp_fault(BYTE_T dp_id, INT_T val)
{
    if(g_TY_sync_time_ok == DEF_COM_FALSE)
    {
        return;
    }

    tuya_ipc_dp_report(NULL, dp_id,PROP_BITMAP,&val,1);
}

VOID respone_dp_bool(BYTE_T dp_id, BOOL_T true_false)
{
    if(g_TY_sync_time_ok == DEF_COM_FALSE)
    {
        return;
    }

    tuya_ipc_dp_report(NULL, dp_id,PROP_BOOL,&true_false,1);
}

VOID respone_dp_enum(BYTE_T dp_id, CHAR_T *p_val_enum)
{
    if(g_TY_sync_time_ok == DEF_COM_FALSE)
    {
        return;
    }

    tuya_ipc_dp_report(NULL, dp_id,PROP_ENUM,p_val_enum,1);
}

INT_T respone_dp_str(BYTE_T dp_id, CHAR_T *p_val_str)
{
    int ret;

    if(g_TY_sync_time_ok == DEF_COM_FALSE)
    {
        return -1;
    }

    ret = tuya_ipc_dp_report(NULL, dp_id,PROP_STR,p_val_str,1);
    return ret;
}

INT_T respone_dp_raw(BYTE_T dp_id, UCHAR_T *p_raw,INT_T len)
{
    int ret;

    if(g_TY_sync_time_ok == DEF_COM_FALSE)
    {
        return -1;
    }

    ret = dev_report_dp_raw_sync(NULL,dp_id,p_raw,len,1);
    return ret;
}

//------------------------------------------
STATIC BOOL_T check_dp_bool_invalid(IN TY_OBJ_DP_S *p_obj_dp)
{
    if((p_obj_dp == NULL) || (p_obj_dp->type != PROP_BOOL))
    {
        PRT_ERR(DEF_TAG,"error! input is null\n");
        return FALSE;
    }

    if(p_obj_dp->value.dp_bool == 0)
    {
        return FALSE;
    }
    else if(p_obj_dp->value.dp_bool == 1)
    {
        return TRUE;
    }
    else
    {
        PRT_WARN(DEF_TAG,"Error!! type invalid %d\n", p_obj_dp->value.dp_bool);
        return FALSE;
    }
}

//------------------------------------------
INT_T check_dp_value_invalid(IN TY_OBJ_DP_S *p_obj_dp)
{
    if((p_obj_dp == NULL) || (p_obj_dp->type != PROP_VALUE))
    {
        PRT_ERR(DEF_TAG,"error! input isn't PROP_VALUE\n");
        return 0;
    }

    return p_obj_dp->value.dp_value;
}

//------------------------------------------

#ifdef TUYA_DP_SLEEP_MODE
STATIC VOID handle_DP_SLEEP_MODE(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T sleep_mode = check_dp_bool_invalid(p_obj_dp);

    IPC_APP_set_sleep_mode(sleep_mode);
    sleep_mode = IPC_APP_get_sleep_mode();

    respone_dp_bool(TUYA_DP_SLEEP_MODE, sleep_mode);
}
#endif

#ifdef TUYA_DP_LIGHT
STATIC VOID handle_DP_LIGHT(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T light_on_off = check_dp_bool_invalid(p_obj_dp);

    IPC_APP_set_light_onoff(light_on_off);
    light_on_off = IPC_APP_get_light_onoff();

    respone_dp_bool(TUYA_DP_LIGHT, light_on_off);
}
#endif

#ifdef TUYA_DP_FLIP
STATIC VOID handle_DP_FLIP(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T flip_on_off = check_dp_bool_invalid(p_obj_dp);

    IPC_APP_set_flip_onoff(flip_on_off);
    flip_on_off = IPC_APP_get_flip_onoff();

    respone_dp_bool(TUYA_DP_FLIP, flip_on_off);
}
#endif

#ifdef TUYA_DP_WATERMARK
STATIC VOID handle_DP_WATERMARK(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T watermark_on_off = check_dp_bool_invalid(p_obj_dp);

    IPC_APP_set_watermark_onoff(watermark_on_off);
    watermark_on_off = IPC_APP_get_watermark_onoff();
    printf("IPC_APP_set_watermark_onoff = %d \n",watermark_on_off);

    respone_dp_bool(TUYA_DP_WATERMARK, watermark_on_off);
}
#endif

#ifdef TUYA_DP_NIGHT_MODE
STATIC VOID handle_DP_NIGHT_MODE(IN TY_OBJ_DP_S *p_obj_dp)
{
    if( (p_obj_dp == NULL) || (p_obj_dp->type != PROP_ENUM))
    {
        printf("Error!! type invalid %d \r\n", p_obj_dp->type);
        return;
    }
    CHAR_T tmp_str[2] = {0};
    tmp_str[0] = '0' + p_obj_dp->value.dp_enum;

    IPC_APP_set_night_mode(tmp_str);
    CHAR_T *p_night_mode = IPC_APP_get_night_mode();

    respone_dp_enum(TUYA_DP_NIGHT_MODE, p_night_mode);
}
#endif


#ifdef TUYA_DP_ALARM_FUNCTION
STATIC VOID handle_DP_ALARM_FUNCTION(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T alarm_on_off = check_dp_bool_invalid(p_obj_dp);

    IPC_APP_set_alarm_function_onoff(alarm_on_off);
    alarm_on_off = IPC_APP_get_alarm_function_onoff();

    respone_dp_bool(TUYA_DP_ALARM_FUNCTION, alarm_on_off);
}
#endif

#ifdef TUYA_DP_ALARM_SENSITIVITY
STATIC VOID handle_DP_ALARM_SENSITIVITY(IN TY_OBJ_DP_S *p_obj_dp)
{
    if( (p_obj_dp == NULL) || (p_obj_dp->type != PROP_ENUM) )
    {
        printf("Error!! type invalid %d \r\n", p_obj_dp->type);
        return;
    }

    CHAR_T tmp_str[2] = {0};
    tmp_str[0] = '0' + p_obj_dp->value.dp_enum;

    IPC_APP_set_alarm_sensitivity(tmp_str);
    CHAR_T *p_sensitivity = IPC_APP_get_alarm_sensitivity();

    respone_dp_enum(TUYA_DP_ALARM_SENSITIVITY, p_sensitivity);
}
#endif

#ifdef TUYA_DP_DEVICE_RESTART
STATIC VOID handle_DP_DEVICE_RESTART(IN TY_OBJ_DP_S *p_obj_dp)
{
    PRT_INFO(DEF_TAG,"dev reboot now\n");
    Ext_InSdkTcpSystem_Cmd("reboot");
}
#endif

#ifdef TUYA_DP_MOTION_TIMER_SETTING
STATIC VOID handle_DP_MOTION_TIMER_SETTING(IN TY_OBJ_DP_S *p_obj_dp)
{
    if((p_obj_dp == NULL) || (p_obj_dp->type != PROP_STR))
    {
        printf("Error!! type invalid %d \r\n", p_obj_dp->type);
        return;
    }
    PRT_INFO(DEF_TAG,"time: %s\n",p_obj_dp->value.dp_str);

    CHAR_T s_str[12];
    CHAR_T g_str[12];

    memset(s_str,0,sizeof(s_str));
    memset(g_str,0,sizeof(g_str));

    strncpy(s_str,p_obj_dp->value.dp_str,sizeof(s_str));

    IPC_APP_set_motion_timer_setting(s_str);
    IPC_APP_get_motion_timer_setting(g_str);

    respone_dp_str(TUYA_DP_MOTION_TIMER_SETTING,g_str);
}
#endif

#ifdef TUYA_DP_MOTION_TIMER_SWITCH
STATIC VOID handle_DP_MOTION_TIMER_SWITCH(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T g_val;
    BOOL_T val = check_dp_bool_invalid(p_obj_dp);

    PRT_INFO(DEF_TAG,"switch: %d\n",val);
    IPC_APP_set_motion_timer_switch(val);
    g_val = IPC_APP_get_motion_timer_switch();

    respone_dp_bool(TUYA_DP_MOTION_TIMER_SWITCH,g_val);
}
#endif

#ifdef TUYA_DP_ALARM_ZONE_ENABLE
STATIC VOID handle_DP_ALARM_ZONE_ENABLE(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T alarm_zone_enable = check_dp_bool_invalid(p_obj_dp);
    IPC_APP_set_alarm_zone_onoff(alarm_zone_enable);
    respone_dp_bool(TUYA_DP_ALARM_ZONE_ENABLE, IPC_APP_get_alarm_zone_onoff());
}
#endif

#ifdef TUYA_DP_ALARM_ZONE_DRAW
STATIC VOID handle_DP_ALARM_ZONE_DRAW(IN TY_OBJ_DP_S *p_dp_json)
{
    if(p_dp_json == NULL)
    {
        printf("Error!! type invalid\r\n");
        return;
    }

    IPC_APP_set_alarm_zone_draw(p_dp_json->value.dp_str);
    respone_dp_str(TUYA_DP_ALARM_ZONE_DRAW, IPC_APP_get_alarm_zone_draw());
}
#endif

#ifdef TUYA_DP_ALARM_INTERVAL
STATIC VOID handle_DP_ALARM_INTERVAL(IN TY_OBJ_DP_S *p_obj_dp)
{
    if( (p_obj_dp == NULL) || (p_obj_dp->type != PROP_ENUM) )
    {
        printf("Error!! type invalid %d \r\n", p_obj_dp->type);
        return;
    }

    INT_T val = p_obj_dp->value.dp_enum;
    CHAR_T tmp_str[4];

    memset(tmp_str,0,sizeof(tmp_str));

    switch(val)
    {
    case 0 :
        sprintf(tmp_str,"%d",1);
        break;

    case 1 :
        sprintf(tmp_str,"%d",3);
        break;

    case 2 :
        sprintf(tmp_str,"%d",5);
        break;

    default:
        break;
    }

    IPC_APP_set_alarm_interval(tmp_str);
    CHAR_T *byte = IPC_APP_get_alarm_interval();

    respone_dp_enum(TUYA_DP_ALARM_INTERVAL, byte);
}
#endif

#ifdef TUYA_DP_SD_STATUS_ONLY_GET
STATIC VOID handle_DP_SD_STATUS_ONLY_GET(IN TY_OBJ_DP_S *p_obj_dp)
{
    INT_T sd_status = IPC_APP_get_sd_status();

    respone_dp_value(TUYA_DP_SD_STATUS_ONLY_GET, sd_status);
}
#endif

#ifdef TUYA_DP_SD_STORAGE_ONLY_GET
STATIC VOID handle_DP_SD_STORAGE_ONLY_GET(IN TY_OBJ_DP_S *p_obj_dp)
{
    CHAR_T tmp_str[128] = {0};

    UINT_T total = 100;
    UINT_T used = 0;
    UINT_T empty = 100;

    tuya_ipc_sd_get_capacity(&total, &used, &empty);
    IPC_APP_get_sd_storage(&total, &used, &empty);

    //"total capacity|Current usage|remaining capacity"
    snprintf(tmp_str, 100, "%d|%d|%d", total, used, empty);
    respone_dp_str(TUYA_DP_SD_STORAGE_ONLY_GET, tmp_str);
}
#endif

#ifdef TUYA_DP_SD_RECORD_ENABLE
STATIC VOID handle_DP_SD_RECORD_ENABLE(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T sd_record_on_off = check_dp_bool_invalid(p_obj_dp);

    IPC_APP_set_sd_record_onoff(sd_record_on_off);
    sd_record_on_off = IPC_APP_get_sd_record_onoff();

    respone_dp_bool(TUYA_DP_SD_RECORD_ENABLE, sd_record_on_off);
}
#endif

#ifdef TUYA_DP_SD_RECORD_MODE
STATIC VOID handle_DP_SD_RECORD_MODE(IN TY_OBJ_DP_S *p_obj_dp)
{
    if( (p_obj_dp == NULL) || (p_obj_dp->type != PROP_ENUM) )
    {
        printf("Error!! type invalid %d \r\n", p_obj_dp->type);
        return;
    }

    UINT_T val = p_obj_dp->value.dp_enum;
    PRT_INFO(DEF_TAG,"mode: %d\n",val);

    IPC_APP_set_sd_record_mode(val);
    UINT_T mode = IPC_APP_get_sd_record_mode();
    CHAR_T sMode[2] = {0};

    snprintf(sMode,2,"%d",mode);
    respone_dp_enum(TUYA_DP_SD_RECORD_MODE,sMode);
}
#endif

#ifdef TUYA_DP_SD_UMOUNT
STATIC VOID handle_DP_SD_UMOUNT(IN TY_OBJ_DP_S *p_obj_dp)
{
    BOOL_T umount_result = IPC_APP_unmount_sd_card();
    respone_dp_bool(TUYA_DP_SD_UMOUNT, umount_result);
}
#endif

#ifdef TUYA_DP_SD_FORMAT
STATIC VOID handle_DP_SD_FORMAT(IN TY_OBJ_DP_S *p_obj_dp)
{
    IPC_APP_format_sd_card();
    respone_dp_bool(TUYA_DP_SD_FORMAT, TRUE);
}
#endif

#ifdef TUYA_DP_SD_FORMAT_STATUS_ONLY_GET
STATIC VOID handle_DP_SD_FORMAT_STATUS_ONLY_GET(IN TY_OBJ_DP_S *p_obj_dp)
{
    INT_T progress = IPC_APP_get_sd_format_status();
    respone_dp_value(TUYA_DP_SD_FORMAT_STATUS_ONLY_GET, progress);
}
#endif

#ifdef TUYA_DP_PTZ_CONTROL
STATIC VOID handle_DP_PTZ_CONTROL(IN TY_OBJ_DP_S *p_obj_dp)
{
    if( (p_obj_dp == NULL) || (p_obj_dp->type != PROP_ENUM) )
    {
        printf("Error!! type invalid %d \r\n", p_obj_dp->type);
        return;
    }

    //dp 119 format: {"range":["1","2","3","4","5","6","7","0"],"type":"enum"}
    UINT_T dp_directions[10] = {0,1,2,3,4,5,6,7,8,9};
    UINT_T direction = dp_directions[p_obj_dp->value.dp_enum];
    CHAR_T tmp_str[2] = {0};
    snprintf(tmp_str,2,"%d",direction);    
    IPC_APP_ptz_start_move(tmp_str);
    respone_dp_enum(TUYA_DP_PTZ_CONTROL,tmp_str);
}
#endif

#ifdef TUYA_DP_PTZ_STOP
STATIC VOID handle_DP_PTZ_STOP(IN TY_OBJ_DP_S *p_obj_dp)
{
    IPC_APP_ptz_stop_move();
    respone_dp_bool(TUYA_DP_PTZ_STOP, TRUE);
}
#endif

#ifdef TUYA_DP_PTZ_CHECK
STATIC VOID handle_DP_PTZ_CHECK(IN TY_OBJ_DP_S *p_obj_dp)
{
    IPC_APP_ptz_check();
    respone_dp_bool(TUYA_DP_PTZ_CHECK, TRUE);
}
#endif

#ifdef TUYA_DP_SOUND_SENSITIVITY
STATIC VOID handle_DP_SOUND_SENSITIVITY(IN TY_OBJ_DP_S *p_obj_dp)
{
    if( (p_obj_dp == NULL) || (p_obj_dp->type != PROP_ENUM) )
    {
        printf("Error!! type invalid %d \r\n", p_obj_dp->type);
        return;
    }

    UINT_T val = p_obj_dp->value.dp_enum;

    IPC_APP_set_sound_sensitivity(val);
    CHAR_T *p_sensitivity = IPC_APP_get_sound_sensitivity();

    respone_dp_enum(TUYA_DP_SOUND_SENSITIVITY,p_sensitivity);
}
#endif

#ifdef TUYA_DP_IPC_CONTRAST
STATIC VOID handle_DP_IPC_CONTRAST(IN TY_OBJ_DP_S *p_obj_dp)
{
    INT_T val = check_dp_value_invalid(p_obj_dp);

    IPC_APP_set_ipc_contrast(val);
    val = IPC_APP_get_ipc_contrast();
    respone_dp_value(TUYA_DP_IPC_CONTRAST, val);
}
#endif

#ifdef TUYA_DP_IPC_BRIGHT
STATIC VOID handle_DP_IPC_BRIGHT(IN TY_OBJ_DP_S *p_obj_dp)
{
    INT_T val = check_dp_value_invalid(p_obj_dp);

    IPC_APP_set_ipc_bright(val);
    val = IPC_APP_get_ipc_bright();
    respone_dp_value(TUYA_DP_IPC_BRIGHT, val);
}
#endif

#ifdef TUYA_DP_SET_SPK_VOLUME
STATIC VOID handle_DP_SET_SPK_VOLUME(IN TY_OBJ_DP_S *p_obj_dp)
{
    INT_T ret_vol;
    INT_T volume = check_dp_value_invalid(p_obj_dp);

    if(volume == TUYA_DP_SET_SPK_VOLUME)
    {
        ret_vol = IPC_APP_get_spk_volume();
    }
    else
    {
        IPC_APP_set_spk_volume(volume);
        ret_vol = IPC_APP_get_spk_volume();
    }

    respone_dp_value(TUYA_DP_SET_SPK_VOLUME,ret_vol);
}
#endif

#ifdef TUYA_DP_AP_MODE
STATIC VOID handle_DP_AP_MODE(IN TY_OBJ_DP_S *p_dp_json)
{
    if(p_dp_json == NULL )
    {
        printf("Error!! type invalid\r\n");
        return;
    }
    respone_dp_str(TUYA_DP_AP_MODE, IPC_APP_get_ap_mode());
}
#endif

#ifdef TUYA_DP_AP_SWITCH
STATIC VOID handle_DP_AP_SWITCH(IN TY_OBJ_DP_S *p_dp_json)
{
    CHAR_T resp[32] = {0};

    INT_T ap_enable = IPC_APP_set_ap_mode((cJSON *)p_dp_json->value.dp_str);
    if(ap_enable < 0)
    {
        snprintf(resp, 32, "{\\\"ap_enable\\\":0,\\\"errcode\\\":0}");
    }
    else
    {
        snprintf(resp, 32, "{\\\"ap_enable\\\":%d,\\\"errcode\\\":0}",ap_enable);
    }

    respone_dp_str(TUYA_DP_AP_SWITCH, resp);
    if(ap_enable >= 0)
    {
        change_ap_process();
    }
}
#endif

#ifdef TUYA_DP_RGB_LED_COLOUR
STATIC VOID handle_DP_RGB_LED_COLOUR(IN TY_OBJ_DP_S *p_obj_dp)
{
    INT_T val = check_dp_value_invalid(p_obj_dp);
    INT_T ret_vol;

    if(val == TUYA_DP_RGB_LED_COLOUR)
    {
        ret_vol = IPC_APP_get_rgb_led_colour();
    }
    else
    {
        IPC_APP_set_rgb_led_colour(val);
        ret_vol = IPC_APP_get_rgb_led_colour();
    }

    respone_dp_value(TUYA_DP_RGB_LED_COLOUR,ret_vol);
}
#endif

#ifdef TUYA_DP_WIRELESS_ELECTRICITY
STATIC VOID handle_DP_WIRELESS_ELECTRICITY(IN TY_OBJ_DP_S *p_obj_dp)
{
    INT_T ret_vol = check_dp_value_invalid(p_obj_dp);
    ret_vol = IPC_APP_get_wireless_electricity();

    async_respone_dp_value(TUYA_DP_WIRELESS_ELECTRICITY,ret_vol);
}
#endif

typedef VOID (*TUYA_DP_HANDLER)(IN TY_OBJ_DP_S *p_obj_dp);
typedef struct
{
    BYTE_T dp_id;
    TUYA_DP_HANDLER handler;
}TUYA_DP_INFO_S;

STATIC TUYA_DP_INFO_S s_dp_table[] =
{
#ifdef TUYA_DP_SLEEP_MODE
    {TUYA_DP_SLEEP_MODE,handle_DP_SLEEP_MODE},
#endif

#ifdef TUYA_DP_LIGHT
    {TUYA_DP_LIGHT,handle_DP_LIGHT},
#endif

#ifdef TUYA_DP_FLIP
    {TUYA_DP_FLIP,handle_DP_FLIP},
#endif

#ifdef TUYA_DP_WATERMARK
    {TUYA_DP_WATERMARK,handle_DP_WATERMARK},
#endif

#ifdef TUYA_DP_NIGHT_MODE
    {TUYA_DP_NIGHT_MODE,handle_DP_NIGHT_MODE},
#endif

#ifdef TUYA_DP_ALARM_FUNCTION
    {TUYA_DP_ALARM_FUNCTION,handle_DP_ALARM_FUNCTION},
#endif

#ifdef TUYA_DP_ALARM_SENSITIVITY
    {TUYA_DP_ALARM_SENSITIVITY,handle_DP_ALARM_SENSITIVITY},
#endif

#ifdef TUYA_DP_DEVICE_RESTART
    {TUYA_DP_DEVICE_RESTART,handle_DP_DEVICE_RESTART},
#endif

#ifdef TUYA_DP_MOTION_TIMER_SETTING
    {TUYA_DP_MOTION_TIMER_SETTING,handle_DP_MOTION_TIMER_SETTING},
#endif

#ifdef TUYA_DP_MOTION_TIMER_SWITCH
    {TUYA_DP_MOTION_TIMER_SWITCH,handle_DP_MOTION_TIMER_SWITCH},
#endif

#ifdef TUYA_DP_ALARM_ZONE_ENABLE
    {TUYA_DP_ALARM_ZONE_ENABLE,handle_DP_ALARM_ZONE_ENABLE},
#endif

#ifdef TUYA_DP_ALARM_ZONE_DRAW
    {TUYA_DP_ALARM_ZONE_DRAW,handle_DP_ALARM_ZONE_DRAW},
#endif

#ifdef TUYA_DP_ALARM_INTERVAL
    {TUYA_DP_ALARM_INTERVAL,handle_DP_ALARM_INTERVAL},
#endif

#ifdef TUYA_DP_SD_STATUS_ONLY_GET
    {TUYA_DP_SD_STATUS_ONLY_GET,handle_DP_SD_STATUS_ONLY_GET},
#endif

#ifdef TUYA_DP_SD_STORAGE_ONLY_GET
    {TUYA_DP_SD_STORAGE_ONLY_GET,handle_DP_SD_STORAGE_ONLY_GET},
#endif

#ifdef TUYA_DP_SD_RECORD_ENABLE
    {TUYA_DP_SD_RECORD_ENABLE,handle_DP_SD_RECORD_ENABLE},
#endif

#ifdef TUYA_DP_SD_RECORD_MODE
    {TUYA_DP_SD_RECORD_MODE,handle_DP_SD_RECORD_MODE},
#endif

#ifdef TUYA_DP_SD_UMOUNT
    {TUYA_DP_SD_UMOUNT,handle_DP_SD_UMOUNT},
#endif

#ifdef TUYA_DP_SD_FORMAT
    {TUYA_DP_SD_FORMAT,handle_DP_SD_FORMAT},
#endif

#ifdef TUYA_DP_SD_FORMAT_STATUS_ONLY_GET
    {TUYA_DP_SD_FORMAT_STATUS_ONLY_GET,handle_DP_SD_FORMAT_STATUS_ONLY_GET},
#endif

#ifdef TUYA_DP_PTZ_CONTROL
    {TUYA_DP_PTZ_CONTROL,handle_DP_PTZ_CONTROL},
#endif

#ifdef TUYA_DP_PTZ_STOP
    {TUYA_DP_PTZ_STOP,handle_DP_PTZ_STOP},
#endif

#ifdef TUYA_DP_PTZ_CHECK
    {TUYA_DP_PTZ_CHECK,handle_DP_PTZ_CHECK},
#endif

#ifdef TUYA_DP_SOUND_SENSITIVITY
    {TUYA_DP_SOUND_SENSITIVITY,handle_DP_SOUND_SENSITIVITY},
#endif

#ifdef TUYA_DP_IPC_CONTRAST
    {TUYA_DP_IPC_CONTRAST,handle_DP_IPC_CONTRAST},
#endif

#ifdef TUYA_DP_IPC_BRIGHT
    {TUYA_DP_IPC_BRIGHT,handle_DP_IPC_BRIGHT},
#endif

#ifdef TUYA_DP_SET_SPK_VOLUME
    {TUYA_DP_SET_SPK_VOLUME,handle_DP_SET_SPK_VOLUME},
#endif

#ifdef TUYA_DP_AP_MODE
    {TUYA_DP_AP_MODE,handle_DP_AP_MODE},
#endif

#ifdef TUYA_DP_AP_SWITCH
    {TUYA_DP_AP_SWITCH,handle_DP_AP_SWITCH},
#endif

#ifdef TUYA_DP_RGB_LED_COLOUR
    {TUYA_DP_RGB_LED_COLOUR,handle_DP_RGB_LED_COLOUR},
#endif
};

VOID IPC_APP_handle_dp_cmd_objs(IN CONST TY_RECV_OBJ_DP_S *dp_rev)
{
    TY_OBJ_DP_S *dp_data = (TY_OBJ_DP_S *)(dp_rev->dps);
    UINT_T cnt = dp_rev->dps_cnt;
    INT_T table_idx = 0;
    INT_T table_count = ( sizeof(s_dp_table) / sizeof(s_dp_table[0]) );
    INT_T index = 0;
    for(index = 0; index < cnt; index++)
    {
        TY_OBJ_DP_S *p_dp_obj = dp_data + index;

        for(table_idx = 0; table_idx < table_count; table_idx++)
        {
            if(s_dp_table[table_idx].dp_id == p_dp_obj->dpid)
            {
                s_dp_table[table_idx].handler(p_dp_obj);
                break;
            }
        }
    }
}

typedef VOID (*TUYA_RAW_DP_HANDLER)(IN CONST TY_RECV_RAW_DP_S *p_obj_dp);
typedef struct
{
    BYTE_T dp_id;
    TUYA_RAW_DP_HANDLER handler;
}TUYA_RAW_DP_INFO_S;

STATIC TUYA_RAW_DP_INFO_S s_raw_dp_table[] =
{
};

VOID IPC_APP_handle_raw_dp_cmd_objs(IN CONST TY_RECV_RAW_DP_S *dp_rev)
{
    INT_T table_idx = 0;
    INT_T table_count = (sizeof(s_raw_dp_table) / sizeof(s_raw_dp_table[0]));

    for(table_idx = 0; table_idx < table_count; table_idx++)
    {
        if(s_raw_dp_table[table_idx].dp_id == dp_rev->dpid)
        {
            s_raw_dp_table[table_idx].handler(dp_rev);
            break;
        }
    }
}

VOID IPC_APP_handle_dp_query_objs(IN CONST TY_DP_QUERY_S *dp_query)
{
    INT_T table_idx = 0;
    INT_T table_count = ( sizeof(s_dp_table) / sizeof(s_dp_table[0]) );
    INT_T index = 0;
    for(index = 0; index < dp_query->cnt; index++)
    {
        for(table_idx = 0; table_idx < table_count; table_idx++)
        {
            if(s_dp_table[table_idx].dp_id == dp_query->dpid[index])
            {
                s_dp_table[table_idx].handler(NULL);
                break;
            }
        }
    }
}

#if defined(TUYA_EXTEND_DEVELOP)
OPERATE_RET APP_DP_VALUE_CMD_PROC(IN CONST INT_T dp_id, IN CONST INT_T dp_value,OUT  INT_T * dp_report_value)
{
    OPERATE_RET ret = 0;
    return ret;
}

OPERATE_RET APP_DP_BOOL_CMD_PROC(IN CONST INT_T dp_id, IN CONST BOOL_T dp_value,OUT  BOOL_T * dp_report_value )
{
    OPERATE_RET ret = 0;

    switch(dp_id)
    {
    case TUYA_DP_LIGHT:
        break;

    case TUYA_DP_ALARM_FUNCTION:
        break;

    case TUYA_DP_SD_RECORD_ENABLE:
        break;

    default :
    {
        ret = -1;
    }
        break;
    }
    return ret;
}

OPERATE_RET APP_DP_STR_CMD_PROC(IN CONST INT_T dp_id, IN CONST CHAR_T *dp_value,OUT  CHAR_T * dp_report_value)
{
    OPERATE_RET ret = 0;

    switch (dp_id)
    {
    case TUYA_DP_SD_STORAGE_ONLY_GET:
        break;

    default:
    {
        ret = -1;
    }
        break;
    }
    return ret;
}

OPERATE_RET APP_DP_ENUM_CMD_PROC(IN CONST INT_T dp_id, IN CONST UINT_T dp_value,OUT  CHAR_T * dp_report_value)
{
    OPERATE_RET ret = 0;
    switch (dp_id)
    {
    case TUYA_DP_SD_RECORD_MODE:
        break;

    default:
    {
        ret = -1;
    }
        break;
    }
    return ret;
}

OPERATE_RET APP_DP_BITMAP_CMD_PROC(IN CONST INT_T dp_id, IN CONST UINT_T dp_value,OUT  UINT_T * dp_report_value)
{
    OPERATE_RET ret = 0;
    return ret;
}
#endif
